<template>
	<view>
		<form>
			<view v-if="form.pid == 0" class="cu-form-group">
				<view class="title">分数</view>
				<view class="flex-sub text-xl">
					<text v-for="item in 5" @click="form.star = item"
						:class="item <= form.star ? 'text-red':'text-gray'"
						class="cuIcon-favorfill margin-right-xs"></text>
				</view>
			</view>
			<view class="cu-bar bg-white solid-top">
				<view class="action title">
					图片
				</view>
				<view class="action">
					{{form.pic.length}}/9
				</view>
			</view>
			<view class="cu-form-group">
				<Upload v-model="form.pic" max="9"></Upload>
			</view>
			<view class="cu-bar bg-white solid-top">
				<view class="action title">
					内容
				</view>
			</view>
			<view class="cu-form-group">
				<textarea maxlength="-1" style="margin-top: 0;" v-model="form.content" placeholder="请输入评价内容"></textarea>
			</view>
		</form>
		<view class="foot-but-h"></view>
		<view class="foot-but">
			<view @click="onSubmit" class="cu-btn block bg-green lg">
				保存
			</view>
		</view>
	</view>
</template>

<script>
	import CommentApi from "@/api/comment"
	import Upload from "@/pages/component/upload.vue"
	export default {
		components: {
			Upload
		},
		data() {
			return {
				form: {
					id: 0,
					order_id: 0,
					pid: 0,
					star: 5,
					pic: [],
					content: '',
				},
				data: {},
			}
		},
		onLoad: function(option) {
			if (option.order_id) {
				this.form.order_id = option.order_id
			} else if (option.pid) {
				this.form.pid = option.pid
				this.getData();
			}
		},
		methods: {
			getData() {
				CommentApi.get(this.form.pid).then((res) => {
					this.data = res;
					this.form.pid=this.data.id
					this.form.order_id = this.data.order_id
				})
			},
			onSubmit() {
				if (!this.form.content) {
					uni.showToast({
						title: "请输入评论内容",
						icon: 'none'
					});
					return;
				}
				if (this.form.id) {
					CommentApi.update(this.form).then(() => {
						this.onSuccess()
					})
				} else {
					CommentApi.add(this.form).then(() => {
						this.onSuccess()
					})
				}
			},
			onSuccess() {
				uni.showToast({
					title: "保存成功",
				});
				uni.navigateBack({
					delta: 1
				});
			},
		}
	}
</script>

<style>

</style>
